<!DOCTYPE html>
<html lang="en" class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">

<head>
<meta charset="UTF-8">
<title>ADD UserMessage</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" th:href="@{/css/font.css}">
<link rel="stylesheet" th:href="@{/css/xadmin.css}">
<script type="text/javascript" th:src="@{/lib/layui/layui.js}"
	charset="utf-8"></script>
<script type="text/javascript" th:src="@{/js/xadmin.js}"></script>

</head>
<body>
	<div class="layui-fluid">
		<div class="layui-row">
			<form class="layui-form" id="fm" enctype="multipart/form-data">
				<div class="layui-form-item">
					<label for="userName" class="layui-form-label"> <span
						class="x-red">*</span>用户姓名
					</label>
					<div class="layui-input-inline">
						<input type="text" id="userName" name="userName" required=""
							lay-verify="userName" autocomplete="off" class="layui-input">
					</div>
					<div class="layui-form-mid layui-word-aux">
						<span class="x-red">*</span>将会成为您唯一的登入名
					</div>
				</div>

				<div class="layui-form-item">
					<label for="userPassword" class="layui-form-label"> <span
						class="x-red">*</span>密&emsp;&emsp;码
					</label>
					<div class="layui-input-inline">
						<input type="password" id="userPassword" name="userPassword"
							required="" lay-verify="userPassword" autocomplete="off"
							class="layui-input">
					</div>
					<div class="layui-form-mid layui-word-aux">6到16个字符</div>
				</div>

				<div class="layui-form-item">
					<label for="reUserPassword" class="layui-form-label"> <span
						class="x-red">*</span>确认密码
					</label>
					<div class="layui-input-inline">
						<input type="password" id="reUserPassword" name="reUserPassword"
							required="" lay-verify="reUserPassword" autocomplete="off"
							class="layui-input">
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-upload">
						<button type="button" class="layui-btn" id="portrait_btn" name="portrait_btn">上传图片</button>
						<input type="hidden" id="img_url" name="portrait" value=""/>
						<div class="layui-upload-list">
							<img alt="这是图片" class="layui-upload-img" id="portraitImg" src="">
							<p id="portraitText"></p>
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label" id="sexRadio">性&emsp;&emsp;别</label>
					<div class="layui-input-block">
						<input class="radio" type="radio" name="sex" value="男" title="男"
							checked> <input class="radio" type="radio" name="sex"
							value="女" title="女">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="userAddress" class="layui-form-label"> <span
						class="x-red">*</span>收货地址
					</label>
					<div class="layui-input-inline">
						<input type="text" id="userAddress" name="userAddress" required=""
							lay-verify="required" autocomplete="off" class="layui-input">
					</div>

				</div>
				<div class="layui-form-item">
					<label for="userPhone" class="layui-form-label"> <span
						class="x-red">*</span>联系方式
					</label>
					<div class="layui-input-inline">
						<input type="text" id="userPhone" name="userPhone" required=""
							lay-verify="phone" autocomplete="off" class="layui-input">
					</div>
					<div class="layui-form-mid layui-word-aux">11位的有效电话号码</div>
				</div>
				<div class="layui-form-item">
					<label for="signature" class="layui-form-label"> <span
						class="x-red">*</span>个性签名
					</label>
					<div class="layui-input-block">
						<textarea id="signature" name="signature" placeholder="请输入内容"
							class="layui-textarea"></textarea>
					</div>
				</div>

				<div class="layui-form-item">
					<label for="L_repass" class="layui-form-label"></label>
					<button id="L_repass" class="layui-btn" lay-filter="add"
						lay-submit="">增加</button>
				</div>
			</form>
		</div>
	</div>

	<script>
		layui.use([ 'form', 'layer', 'jquery', 'upload' ], function() {
			$ = layui.jquery;
			var form = layui.form, layer = layui.layer, upload = layui.upload;

			/*上传图片*/
			var uploadInst = upload.render({
				elem: '#portrait_btn',
				url: '/upload',
				before: function (obj) {
					//预读本地文件示例，不支持ie8
					obj.preview(function (index, file, result) {
						console.log(file);
						// console.log(result);
						$('#portraitImg').attr('src', result); //图片链接（base64）
					});
				},
				done: function (res) {
					//如果上传失败
					if (res.status > 0) {
						return layer.msg('上传失败');
					}
					//上传成功
					console.log(res.code)
					if (res.code === '0') {
						console.log(res.src)
						$('#img_url').attr("value",res.src)
					}
				}
				, error: function () {
					//演示失败状态，并实现重传
					var portraitText = $('#portraitText');
					portraitText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
					portraitText.find('.demo-reload').on('click', function () {
						uploadInst.upload();
					});
				}
			});


			//自定义验证规则
			form
					.verify({
						userName : function(value) {
							if (value.length < 5) {
								return '昵称至少得5个字符啊';
							}
						},
						userPassword : [ /(.+){6,12}$/, '密码必须6到12位' ],
						reUserPassword : function(value) {
							if ($('#userPassword').val() !== $(
									'#reUserPassword').val()) {
								return '两次密码不一致';
							}
						}
					});

			//监听提交
			form.on('submit(add)', function(data) {
				console.log(data.field)
				$.ajax({
					url : "/userMessage/add",
					method : 'post',
					data : data.field,
					dataType : 'JSON',
					success : function(res) {
						if (res.code === 0) {
							//弹出提示，并在1秒后进行跳转
							layer.alert("增加成功", {
								icon : 6
							}, function() {
								//关闭当前frame
								xadmin.close();

								// 可以对父窗口进行刷新 
								xadmin.father_reload();
								return false;
							});
						} else {
							layer.msg("新增失败:" + res.msg);
							return false;
						}
					}
				});
				return false;
			});
		})
	</script>


</body>

</html>